<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Validation class for validating that a username does not
 * previously exist in the system
 *
 * @category    RedCross
 * @package     RedCross_Validate
 */
class RedCross_Validate_UniqueEmail extends Zend_Validate_Abstract
{
	private $_member;
	
    /**
     * Validation failure message key for when
     * the email already exists in the system
     * @var string
     */
    const EMAIL_EXISTS = 'emailExists';

    /**
     * Validation failure message template definitions
     * @var array
     */
    protected $_messageTemplates = array(
        self::EMAIL_EXISTS => 'Epostadressen er allerede registrert i systemet',
    );

    /**
     * (non-PHPdoc)
     * @see library/Zend/Validate/Zend_Validate_Interface#isValid($value)
     */
    public function isValid($value)
    {
        $value = (string) $value;
        $this->_setValue($value);

        $query = Doctrine_Query::create()
            ->select('e.id')
            ->from('Core_Model_Email e')
            ->where('e.address = ?', $value)
            ->execute();

        if (!count($query)) {
            return true;
        }
        $this->_error(self::EMAIL_EXISTS);
        return false;
    }
}